﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace MimiDouban.Test
{
    /// <summary>
    /// 用于记录日志的类
    /// </summary>
    public class TestLog
    {
        private static string FILENAME = "log.txt";
        private static TestLog uniqeInstance;

        private StreamWriter file;

        private TestLog()
        {
            if (!File.Exists(FILENAME))
            {
                File.Create(FILENAME).Close();
            }
        }

        public static TestLog getInstance()
        {
            if (uniqeInstance == null)
            {
                uniqeInstance = new TestLog();
            }
            return uniqeInstance;
        }

        public void info(string message)
        {
            string time = DateTime.Now.ToString() + "." + DateTime.Now.Millisecond;
            string threadInfo = Thread.CurrentThread.ManagedThreadId + "-" + Thread.CurrentThread.Name;
            string all = "\r\n************************************************************************\r\n"
                        + "信息:" + time + ": " + threadInfo + "-" + message
                        + "\r\n************************************************************************\r\n";
            lock (this)
            {
                file = File.AppendText(FILENAME);
                file.WriteLine(all);
                file.Flush();
                file.Close();
            }
        }

        public void error(string message)
        {
            string time = DateTime.Now.ToString() + "." + DateTime.Now.Millisecond;
            string threadInfo = Thread.CurrentThread.ManagedThreadId + "-" + Thread.CurrentThread.Name;
            string all = "************************************************************************\r\n"
                        + "错误:" + time + ": " + threadInfo + "-" + message
                        + "\r\n************************************************************************\r\n";
            lock (this)
            {
                file = File.AppendText(FILENAME);
                file.WriteLine(all);
                file.Flush();
                file.Close();
            }
        }

        public void error(string message, Exception e)
        {
            string time = DateTime.Now.ToString() + "." + DateTime.Now.Millisecond;
            string threadInfo = Thread.CurrentThread.ManagedThreadId + "-" + Thread.CurrentThread.Name;
            string all = "************************************************************************\r\n"
                        + "错误:" + time + ": " + threadInfo + "-" + message + "\r\n"
                        + e.GetType().ToString() + ":" + e.Message + "\r\n" + e.StackTrace
                        + "\r\n************************************************************************\r\n";
            lock (this)
            {
                file = File.AppendText(FILENAME);
                file.WriteLine(all);
                file.Flush();
                file.Close();
            }
        }

        public string getAllLogs()
        {
            lock (this)
            {
                return File.ReadAllText(FILENAME);
            }
        }
    }
}
